$(function () {

    //初始化加载datagrid
    $('#dg1').datagrid({
        method: 'post',
        url: ROOT_PATH + '/workflow/demo/findLeaveApplicationList.shtml',
        queryParams: {
         sort: 'createTime',
         order: 'desc'
         },
        //idField: 'id',
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        sortOrder: true,
        striped: true,
        fit: true,
        columns: [[
            {field: 'id', width: 80, checkbox: true},
            {field: 'type', title: '请假类型', width: 100, sortable: true,
                formatter: function (value, row, index) {
                    if (value == 0) {
                        return '事假';
                    } else {
                        return '婚假';
                    }
                }},
            {field: 'startDate', title: '开始时间', width: 80, sortable: true},
            {field: 'endDate', title: '结束时间', width: 80, sortable: true},
            {field: 'leaveDay', title: '请假天数', width: 80, sortable: true},
            {field: 'content',title: '请假内容'},
            {field: 'flowState',title: '审批状态',
                formatter: function (value, row, index) {
                    //判断审批状态；0:流程已创建，未提交；1:流程审批进行中；7:流程已结束；
                    if(row.workflowInstance==null){
                        return "未提交";
                    }else{
                        var flowState=row.workflowInstance.state;
                        var returnStr="<a onclick=\"flowMonitorByBusinessId('"+row.id+"')\" style='color:blue;'>";
                        returnStr+=flowState==0?'未提交':flowState==1?'审批中':flowState==7?"已结束":'';
                        returnStr+="</a>";
                        return returnStr;
                    }

                }
            },
            {field: 'createUserName', title: '登记人', width: 80, sortable: true},
            {field: 'createTime', title: '登记时间', width: 80, sortable: true}
        ]]
    });
    /***
     * 查询按钮
     */
    $('#btn_search').on("click", function () {
        var params=$('#tb1').find(':input').serializeJson();
        params.sort='createTime';
        params.order='desc';
        $('#dg1').datagrid('load',params);
    });
    /**
     * 打开请假单创建页面
     * @Author: wangqingguo
     * @Date: 2017/3/14
     */
    $('#btn_leaveApplication_add').on('click',function () {
        var index = layer.open({
            type: 2,
            title: '新建请假单',
            area: ['600px', '80%'],
            content: ROOT_PATH + '/workflow/demo/add.shtml'
        });
    });

    /***
     * 提交审批
     * @returns {boolean}
     */
    $('#btn_leaveApplication_submit').on('click',function () {
        var $selected = $('#dg1').datagrid('getSelections');
        if ($selected.length != 1) {
            layer.alert('请选择一个请假单！');
            return false;
        }
        var leaveApplicationId=$selected[0].id;//请假单id
        if($selected[0].workflowInstance!=null){
            layer.alert('业务流程已存在，禁止重复提交！');
            return false;
        }
        var confirmIndex=layer.confirm('是否确定提交审批？', function () {
            layer.close(confirmIndex);
            /***
             * 选择处理人
             */
            WindowManager.openLayer({
                type: 2,
                title: '选择下一环节处理人',
                area: ['370px', '180px'],
                content: ROOT_PATH + '/workflow/flowManagement/toFlowNextUserSelectPage.shtml',
                data:{id:'ff',name:'我是名称'},
                callBack:function(data){//窗口关闭后的回调函数
                    //得到选择的人员
                    if(data==null||data===""){
                        layer.alert("请选择处理人!");
                        return false;
                    }
                    $.ajax({
                        type: "POST",
                        url: ROOT_PATH + '/workflow/demo/submitLeaveApplication.shtml',
                        data: {id: $selected[0].id,nextUserId:data},
                        success: function (result) {
                            result = $.parseJSON(result);
                            if (result.success) {
                                layer.msg(result.message, {time: 1000, icon: 1}, function () {
                                    $('#dg1').datagrid('reload');
                                });
                            } else {
                                layer.msg(result.message);
                            }
                        }
                    });
                }
            });
        });
    });
});
/****
 * 提交审批
 */
function submitVerrifyForm(){

}


